package com.remist.dao.impl.jpa;

import java.util.List;

import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.remist.dao.impl.ProductDao;
import com.remist.model.Product;
import com.remist.model.ProductType;

 
public class ProductDaoImpl    extends JpaDaoSupport    implements ProductDao {

	public Product addProduct(Product productBean) {
		getJpaTemplate() .persist(productBean);
		return productBean;
	}

	public boolean updateProduct(Product productBean) {
		getJpaTemplate().merge(productBean); 
		return false;
	}

	public boolean deleteProduct(Product productBean) {
		Product product = getJpaTemplate().find(Product.class, productBean.getId());
        if (product != null)
        {
            getJpaTemplate().remove(product);
        }
		return true;
	}

	public List<Product> getProductByType(ProductType productType) {
		List<Product> find = getJpaTemplate().find(" SELECT  p FROM Product p WHERE p.typeId="+productType.getId());
		return find;
	}

	public List<Product> getAllProduct(int beginRecord, String whereCondition) {
		List<Product> find = getJpaTemplate().find(" SELECT  p FROM Product p WHERE "+whereCondition);
		return find;
	}

	public Product getProductById(Product productBean) {
		 return getJpaTemplate().find(Product.class, productBean.getId());
	}

}
